{template 'web/_header'}
{template 'tabs'}
{if $operation == 'post'}
    <form id="dataform" action="" method="post" class="form-horizontal form" enctype="multipart/form-data">
        <div class='panel panel-default'>
            <div class='panel-heading'>{if empty($_GPC['id'])}添加数据 (模板id:{$_GPC['typeid']}){else}编辑数据 (模板id:{$_GPC['typeid']} 数据id:{$_GPC['id']}){/if}</div>
            <input type="hidden" name="typeid" value="{$item['id']}"/>
            <div class='panel-body'>
                <div class="alert alert-info">{if empty($_GPC['id'])}您正在向模板:“{$item['title']} (id:{$item['id']})” 添加数据{else}您正在编辑模板id:{$_GPC['typeid']}数据id:{$_GPC['id']}的内容{/if}<br>Tips:主键自动填充只适用于以下格式：00000001(纯数字)、C00000001(字母开头数字结尾) 其他格式请手动填写或者Excel导入。</div>
                <table class="table">
                    <thead>
                        <tr>
                            {loop $item['fields'] $key $name}
                            <th>{$name} ({$key}) {if $key=='key'}主键 {if empty($_GPC['id'])}<a class='btn btn-default btn-add-type' style="float: right;" href="javascript:;" onclick='autonum()' >自动填充</a>{/if}{/if}</th>
                            {/loop}
                            <th style="width: 50px;">操作</th>
                        </tr>
                    </thead>
                    <tbody id="type-items">
                        {template 'tp_data'}
                    </tbody>
                </table>
                {if empty($_GPC['id'])}
                    <div class="input-group " style="width:260px; float: left; margin-left: 8px;">
                        <span class="input-group-addon">增加</span>
                        <input type="text" name="numlist" value="1" class="form-control" style="padding:0px; text-align: center;">
                        <span class="input-group-addon" style="border-left: 0px;">条数据</span>
                        <span class="btn btn-default btn-add-type2" style="float: left; border-radius: 0px 4px 4px 0px; border-left: 0px;" onclick="addType();"><i class="fa fa-plus" title="" ></i> 确认增加</span>
                    </div> 
                    <div class="col-sm-9 col-xs-12" style="float: left; width: auto; ">
                            <a class="btn btn-default btn-add-type" onclick='autonum()' href="javascript:;" style="margin-right: 10px;"><i class="fa fa-angle-double-down" title=""></i> 主键自动填充</a>
                            {ifp 'diyform.data.import'}
                            <a class="btn btn-primary" href="javascript:;" style="margin-right: 10px;" onclick="$('#modal-import').modal()"><i class="fa fa-plus" title=""></i> Excel导入</a>
                            <a class="btn btn-primary" href="{php echo $this->createPluginWebUrl('diyform/import',array('op'=>'temp','id'=>$item['id']))}" style="margin-right: 10px;" ><i class="fa fa-download" title=""></i> 下载Excel模板文件</a>
                            {/if}
                    </div>
                {/if}
            </div>
            
                <div class='panel-footer' style="height:auto; overflow: hidden;">
                    {ifp 'diyform.data.add|diyform.data.edit'}
                    <input type="submit" name="submit" value="保存数据" class="btn btn-primary col-lg-1"  />
                    {/if}
                      {ifp 'diyform.data.view'}
                    <a class="btn btn-default " href="{php echo $this->createPluginWebUrl('diyform/data',array('typeid'=>$item['id']))}" style="margin-left: 10px;"><i class="fa fa-list" title=""></i> 查看已有数据</a>
                    {/if}
                    <input type="hidden" name="token" value="{$_W['token']}" />
                </div>
        </div>
    </form>

        <div id="modal-import" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true" style="width:600px;margin:0px auto;">
                 <form id="importform" class="form-horizontal form" action="{php echo $this->createPluginWebUrl('diyform/import')}" method="post" enctype="multipart/form-data">
                <input type='hidden' name='typeid' value="{$item['id']}" />
                <input type='hidden' name='op' value='import' />
                     
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                        <h3>上传数据</h3>
                    </div>
                    <div class="modal-body">
                          <div class="form-group">
                            <label class="col-xs-12 col-sm-3 col-md-2 control-label" style='width: 150px'>选择EXCEL:</label>
                            <div class="col-sm-9 col-xs-12" style='width: 380px'>
                                   <input type="file" name="excelfile" class="form-control" />
                            </div>
                            <label class="col-xs-12 col-sm-3 col-md-2 control-label" style='width: 150px'>注意:</label>
                            <div class="col-sm-9 col-xs-12" style='width: 380px'>
                                <span style="line-height: 36px; font-size: 12px;">如果遇到数据重复则将进行数据更新（在数据未使用的情况下）</span>
                            </div>
                        </div>
                        <div id="module-menus"></div>
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-primary span2" name="cancelsend" value="yes">确认导入</button>
                        <a href="#" class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</a>
                    </div>
                </div>
            </div>
                </form>
        </div>

<script language='javascript'>
$(function(){
    
    $('#importform').submit(function(){
        if(!$(":input[name=excelfile]").val()){
            alert("您还未选择Excel文件哦~");
            return false;
        }
    })

    $('#dataform').submit(function(){
        var check = true;
        $("input[type=text]").each(function(){
            var val = $(this).val();
            if(!val){
                Tip.focus($(this),'不能为空!');
                check =false;
                return false;
            }
        });
        if(!check){return false;}
        var o={}; // 判断重复
        $("input[mk=1]").each(function(){
            if(!(o[$(this).val()])){
                o[$(this).val()] = true;
            }else{
                var val = $(this).val();
                $("input[mk=1]").each(function(){
                   if($(this).val()==val){
                       $(this).css("border-color","#f01");
                   }else{
                       $(this).css("border-color","#ccc");
                   }
                });
                alert("啊哦，红圈里的数据 不能重复哦~！");
                check =false;
                return false;
            }
        });
        if(!check){return false;}
        return check;
    });
})
    var kw = 1;
    function addType() {
        numlist = $("input[name=numlist]").val();
        if(!$.isInt(numlist)){
            alert("请填写要添加的条数~");
            return false;
        }
        if(numlist>100){
            alert("每次最多增加100条数据哦~");
            return false;
        }
        $(".btn-add-type2").button("loading");
        $.ajax({
            url: "{php echo $this->createPluginWebUrl('diyform/temp',array('op'=>'addtype','addt'=>'data','typeid'=>$_GPC['typeid']))}&kw="+kw+"&numlist="+numlist,
            cache: false
        }).done(function (html) {
            $(".btn-add-type2").button("reset");
            $("#type-items").append(html);
        });
        kw++;
    }    
    function removeType(obj) {
        $(obj).parent().parent().remove();
    } 
   
    function autonum(){
        var val =$.trim( $(":input[mk=1]:first").val() );
        if(val==''){
            Tip.focus($(":input[mk=1]:first"),'请先录入一个值!');
            return;
        }
 
        
        var num =val.replace(/[^0-9]/,'') ;
        var eng = val.replace(num,''); //21097  
        $('.btn-add-type').button('loading');
        $.ajax({
             url: "{php echo $this->createPluginWebUrl('diyform/data')}",
             data: { op:'autonum', num: num ,len: $(":input[mk=1]").length -1,typeid:"{$_GPC['typeid']}" },
             type: "POST",
             dataType:"json",
             success: function(arr){
                 for(var i in arr){
                     $(":input[mk=1]:eq(" + i + ")").val(eng+ arr[i] );
                 }
  $('.btn-add-type').button('reset');
             }
         })
    }
</script>

{elseif $operation == 'display'}
        <!-- 筛选区域 -->
        <div class="panel panel-info">
            <div class="panel-heading">筛选</div>
            <div class="panel-body">
                <form action="./index.php" method="get" class="form-horizontal" role="form">
                    <input type="hidden" name="c" value="site" />
                    <input type="hidden" name="a" value="entry" />
                    <input type="hidden" name="m" value="sz_yi" />
                    <input type="hidden" name="do" value="plugin" />
                    <input type="hidden" name="p" value="diyform" />
                    <input type="hidden" name="method" value="data" />
                    <input type="hidden" name="op" value="display" />
                    <input type="hidden" name="typeid" value="{$type['id']}" />
                    <div class="form-group">
                        <label class="col-xs-12 col-sm-2 col-md-2 col-lg-1 control-label">关键字</label>
                        <div class="col-sm-8 col-lg-9">
                            <input class="form-control" name="keyword" id="" type="text" value="{$_GPC['keyword']}" placeholder="请输入关键字进行搜索">
                        </div>

                    </div>
                    <!--<div class="form-group">-->
                        <!--<label class="col-xs-12 col-sm-2 col-md-2 col-lg-1 control-label">使用状态</label>-->
                        <!--<div class="col-sm-8 col-lg-9">-->
                            <!--<select class='form-control' name='status'>-->
                                <!--<option value=''></option>-->
                                <!--<option value='0' {if $_GPC['status']=='0'}selected{/if}>未使用</option>-->
                                <!--<option value='1' {if $_GPC['status']=='1'}selected{/if}>已使用</option>-->
                            <!--</select>-->
                        <!--</div>-->
                    <!--</div>-->
                    <div class="form-group">
                        <label class="col-xs-12 col-sm-2 col-md-2 col-lg-1 control-label"></label>
                        <div class="col-sm-8 col-lg-9">
                         <button class="btn btn-default"><i class="fa fa-search"></i> 搜索</button>
                        </div>

                    </div>
                </form>
            </div>
        </div>
        <div class='panel panel-default'>
            <div class='panel-heading'> 数据列表(总数:{$total}; 模板名称:{$type['title']})</div>
            <div class='panel-body'>
                <table class="table">
                    <thead>
                        <tr>
                            <th>编号</th>
                            {loop $type['fields'] $key $value}
                                <th style=''>{php echo $value['tp_name']}</th>
                            {/loop}


                            <!--<th>辑编 / 删除</th>-->

                        </tr>
                    </thead>
                    <tbody>
                        {loop $items $item}
                            <tr>
                                <td>{$item['id']}</td>

                                    {php $datas = iunserializer($item['fields'])}
                                    {loop $type['fields'] $key $value}
                                     <td>
                                         {if $value['data_type'] == 0 || $value['data_type'] == 1 || $value['data_type'] == 2 || $value['data_type'] == 6}
                                         {php echo $datas[$key]}

                                         {else if $value['data_type'] == 3}
                                         {if !empty($datas[$key])}
                                         {loop $datas[$key] $k1 $v1}
                                         {php echo $v1}
                                         {/loop}
                                         {/if}

                                         {else if $value['data_type'] == 5}
                                         {if !empty($datas[$key])}
                                         {loop $datas[$key] $k1 $v1}
                                         <a target="_blank" href="{php echo tomedia($v1)}"><img style='width:35px;height:35px;padding:1px;border:1px solid #ccc'  src="{php echo tomedia($v1)}"></a>
                                         {/loop}
                                         {/if}

                                         {else if $value['data_type'] == 7}
                                         {php echo $datas[$key]}

                                         {else if $value['data_type'] == 8}
                                         {if !empty($datas[$key])}
                                         {loop $datas[$key] $k1 $v1}
                                         {php echo $v1}
                                         {/loop}
                                         {/if}

                                         {else if $value['data_type'] == 9}
                                         {php echo $datas[$key]['province']}-{php echo $datas[$key]['city']}
                                         {/if}
                                     </td>
                                    {/loop}

                            </tr>
                        {/loop}
                        {if !empty($pager)}
                            <tr>
                                <td colspan="{php echo $colspan+7}">{$pager}</td>
                            </tr>
                        {/if}
                    </tbody>
                </table>
            </div>
            <div class='panel-footer'>
                    <a class='btn btn-default' href="{php echo $this->createPluginWebUrl('diyform/temp')}"><i class="fa fa-reply"></i> 返回列表</a>
                    {ifp 'diyform.data.add'}
                    <!--<a class='btn btn-default' href="{php echo $this->createPluginWebUrl('diyform/data', array('op' => 'post','typeid'=>$_GPC['typeid']))}"><i class="fa fa-plus"></i> 添加数据</a>-->
                    {/if}
                    {ifp 'diyform.data.export'}
                    <!--<a class='btn btn-primary' href="{php echo $this->createPluginWebUrl('diyform/export', array('typeid'=>$_GPC['typeid']))}"><i class="fa fa-download"></i> 导出已使用数据</a>-->
                    {/if}
            </div>
        </div>
 
{/if}
{template 'common/footer'}
